<?php
include_once "setup.php";
include_once "library.php";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<script type="text/javascript" src="jquery-1.6.2.min.js"></script>
<script type="text/javascript" src="/js/global.js"></script>
<link type="text/css" rel="stylesheet" href="/css/global.css" /> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="imagetoolbar" content="no" />
<title>TPGallery</title>
</head>
<body>
<?php

	// 파라메터 처리
	$path = urldecode($_GET['path']);
	$page = $_GET['page'];
	if ($path == "") $path="/";
	if ($page == "" || !($page > 0)) $page = 1;

	$start_count = $tpg_config["page_list"] * $page - $tpg_config["page_list"];
	$end_count = $start_count + $tpg_config["page_list"];
	
	// Filter
	$path = str_replace("..","",$path);
	while (substr($path,-1) == "/") $path = substr($path,0,strlen($path)-1);

	$dir_path = $tpg_config["prefix_photo_dir"].$path;

	// JPG 파일의 목록 얻기
	$dir_handle = @opendir($dir_path) or die("파일목록 얻기에 실패했습니다.");
	$list = array();
	$dirlist = array();
	if ($path != "" && $path != "/")
		array_push($dirlist, "-*updir");
	
	while(($filename=readdir($dir_handle))!=FALSE) {
		// 시스템 문자셋을 utf-8로 변경
		if (getFileExt($filename) == "jpg") {
			array_push($list,$filename);
		// 디렉토리일 경우 다른 배열에 저장
		} elseif (is_dir($dir_path . "/" . $filename) && $filename != ".." && substr($filename,0,1) != ".")
			array_push($dirlist,$filename);
	}
	closedir($dir_handle);

	sort($list);

	if ($end_count > count($list)) $end_count = count($list);

	// 페이지 항목 처리
	$page_link = "";
	$max_page = count($list) % $tpg_config["page_list"];
	if ($max_page == 0)
		$max_page = count($list) / $tpg_config["page_list"];
	else
		$max_page = ceil(count($list)/$tpg_config["page_list"]);
	$next_page = $page + $tpg_config["page_count"];
	$next_page -= ($next_page - 1) % $tpg_config["page_count"];
	if ($next_page > $max_page) {
		$next_page = $max_page;
	}
	$prev_page = $page - ($page % $tpg_config["page_count"]);
	if ($prev_page < 1) {
		$prev_page = 1;
	}
	for ($i=1;$i<=$tpg_config["page_count"];$i++) {
		$num = floor(($page-1)/$tpg_config["page_count"]) * $tpg_config["page_count"] + $i;
		if ($num > $max_page) break;
		if ($num == $page)
			$page_link .= "<span id=\"current\">" . $num . "</span>\n";
		else
			$page_link .= "<a href=\"" . $PHP_SELF . "?path=" . urlencode($path) . "&page=" . $num . "\">[" . $num . "]</a>\n";
	}
	$page_link = "<a href=\"" . $PHP_SELF . "?path=" . urlencode($path) . "&page=" . $prev_page . "\">prev</a>\n" . $page_link;
	$page_link .= "<a href=\"" . $PHP_SELF . "?path=" . urlencode($path) . "&page=" . $next_page . "\">next</a>";

	// 갤러리 출력
	$gallery = "<ul id=\"subdir\">\n";
	foreach($dirlist as $dirname) {
		$dirpath = $path . "/" . $dirname;
		$dirname = convertEncoding($dirname);
		// 상위 디렉토리 경로 얻기
		if ($dirname == "-*updir") {
			$dirname = "[Up Directory]";
			$dirpath = substr($path,0,strrpos($path, "/"));
		}
		$gallery .= "  <li><a href=\"?path=" . urlencode($dirpath) . "\" class=\"directory\">" . $dirname . "</a></li>";
	}
	$gallery .= "</ul>\n<br /><br />\n";
	$gallery .= "<table id=\"gallery\">\n";
	$col = 1;

	if ($tpg_config["thumbnail"] == true) {
		if (!is_dir($tpg_config["prefix_thum_dir"] . $path))
		{
			@mkdir($tpg_config["prefix_thum_dir"] . $path);
			@chmod($tpg_config["prefix_thum_dir"] . $path,766);
		}
		if (!is_dir($tpg_config["prefix_thum_dir"] . $path) && $tpg_config["thumbnail_noerror"] == false)
			die("썸네일 디렉토리를 생성하지 못했습니다.<br />퍼미션을 조정해 주세요");
	}

	// 표 형태로 출력
	$count = $start_count;
	while (true) {
		$gallery .= "  <tr>\n";
		for ($i=0;$i<$tpg_config["page_col"];$i++) {
			// 데이터가 있고 전부 출력된게 아니라면
			if ($list[$count] && $count < $end_count) {
				// 작은 이미지 사이즈 구하기
				$src = $dir_path . "/" . $list[$count];
				$title = substr($list[$count],0,strlen($list[$count])-4);
				$exif = exif_info($dir_path . "/" . $list[$count]);
				$size = getimagesize($src);
				$width = $size[0];
				$height = $size[1];
				if ($width > $height) {
					$height = $height/($width/$tpg_config["thumbnail_size"]);
					$width = $tpg_config["thumbnail_size"];
				}elseif ($width < $height) {
					$width = $width/($height/$tpg_config["thumbnail_size"]);
					$height = $tpg_config["thumbnail_size"];
				}else{
					$width = $height = $tpg_config["thumbnail_size"];
				}
		
				//$img_src = "view.php?path=" . $path . "&name=" . $list[$count];
				$image_src = convertEncoding($path . "/" . $list[$count]);
				$thum_src = "thumb.php?path=" . urlencode($path) . "&name=" .urlencode($list[$count]);
				
				$gallery .= "    <td valign=\"top\"><a href=\"#\" onclick=\"showImage('photos" . $image_src . "')\"><img src=\"" . $thum_src . "\" width=\"" . $width . "\" height=\"" . $height . "\" border=\"0\" class=\"gimg\" /><br /><p class=\"label\">" . convertEncoding($title) . "</p></a>" . $exif . "</td>\n";
			}else{
				$gallery .= "    <td></td>";
			}
			$count++;
		}
		$gallery .= "  </tr>\n";
		
		if ($count >= $end_count)
			break;
	}
	$gallery .= "  <tr><td colspan=\"" . $tpg_config["page_col"] . "\" class=\"link\">" . $page_link . "</td></tr>\n";
	$gallery .= "</table>";

	echo $gallery;
?>
<div id="popupContact"><a href="#" onclick="disablePopup();return false" alt="클릭하시면 이미지를 닫습니다"><img id="popupImage" border="0" /><br /><span style="color:#000">닫기</span></a>
</div>
<div id="backgroundPopup"></div>
</body>
</html>
